<!--
 * @Author: early-autumn
 * @Date: 2020-05-28 13:46:29
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-05-28 14:13:14
-->
<template>
  <el-dialog
    title="修改医生"
    width="380px"
    :visible="value"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" inline>
      <el-form-item label="选择医生" prop="doctorId">
        <el-select v-model="form.doctorId" placeholder="请选择医生">
          <el-option
            v-for="option in doctorOptions"
            :key="option.userId"
            :value="option.userId"
            :label="option.userName"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { mapState } from 'vuex'
import { listStaff } from '@/api/services/common'
import { updateAppointment } from '@/api/services/appointment'

export default {
  name: 'ModifyDoctor',
  model: {
    prop: 'value',
    event: 'close',
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
      default: () => ({
        id: 0,
        doctorId: '',
      }),
    },
  },
  data() {
    return {
      loading: false,
      form: {
        doctorId: '',
      },
      rules: {
        doctorId: { required: true, message: '请选择医生', trigger: 'blur' },
      },
      doctorList: [],
    }
  },
  computed: {
    ...mapState({
      clinicId: (state) => state.clinic.clinicId,
    }),
    doctorOptions() {
      return [...this.doctorList]
    },
  },
  watch: {
    value(newVal) {
      if (newVal) {
        this.init()
      } else {
        this.$refs.form.resetFields()
      }
    },
  },
  mounted() {
    this.getListStaff()
  },
  methods: {
    init() {
      this.form.doctorId = +this.data.doctorId
    },
    /**
     * 获取医生列表
     */
    async getListStaff() {
      const { data } = await listStaff({
        deptId: +this.clinicId,
        postname: '医生',
      })

      this.doctorList = data
    },
    handleClose() {
      this.$emit('close', false)
    },
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        try {
          if (!valid) {
            return
          }

          this.loading = true

          const { doctorId } = this.form
          const doctorName = this.doctorOptions.reduce((doctorName, doctor) => {
            if (doctor.userId === doctorId) {
              doctorName += doctor.userName
            }
            return doctorName
          }, '')
          await updateAppointment({
            id: this.data.id,
            doctorId,
            doctorName,
          })
          this.msgSuccess('修改成功')
          this.handleClose()
          this.$emit('ok')
        } finally {
          this.loading = false
        }
      })
    },
  },
}
</script>

<style></style>
